package com.chaihu.admin.service.impl.menugroup;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaihu.admin.dao.MenuGroupServiceDao;
import com.chaihu.bean.dto.admin.MenuGroupDto;
import com.chaihu.bean.in.admin.menugroup.QueryMenuGroupsIn;
import com.chaihu.bean.out.admin.menugroup.QueryMenuGroupsOut;
import com.chaihu.bean.result.ResponseResult;
import com.chaihu.intf.admin.menugroup.ListMenuGroupsService;
import com.chaihu.intf.icommon.BaseService;
import com.chaihu.utils.util.BeanConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
public class ListMenuGroupsServiceImpl extends BaseService<ResponseResult<List<QueryMenuGroupsOut>>, QueryMenuGroupsIn> implements ListMenuGroupsService {
    @Autowired
    private MenuGroupServiceDao menuGroupServiceDao;

    @Override
    public ResponseResult<List<QueryMenuGroupsOut>> checkHandle(ResponseResult<List<QueryMenuGroupsOut>> responseResult, QueryMenuGroupsIn in) {
        return responseResult;
    }

    @Override
    public ResponseResult<List<QueryMenuGroupsOut>> doHandle(ResponseResult<List<QueryMenuGroupsOut>> responseResult, QueryMenuGroupsIn in) {
        LambdaQueryWrapper<MenuGroupDto> queryWrapper = Wrappers.lambdaQuery(in);
        if (in.getPageDto() != null) {
            Page<MenuGroupDto> pageQuery = new Page<>(in.getPageDto().getPage(), in.getPageDto().getRow());
            IPage<MenuGroupDto> menuGroupDtoIPage = menuGroupServiceDao.selectPage(pageQuery, queryWrapper);

            List<QueryMenuGroupsOut> menuGroupsOuts = BeanConvertUtil.covertBeanList(menuGroupDtoIPage.getRecords(), QueryMenuGroupsOut.class);
            responseResult.setData(menuGroupsOuts, menuGroupDtoIPage);
        } else {
            List<MenuGroupDto> queryMenuGroupsOuts = menuGroupServiceDao.selectList(queryWrapper);

            List<QueryMenuGroupsOut> menuGroupsOuts = BeanConvertUtil.covertBeanList(queryMenuGroupsOuts, QueryMenuGroupsOut.class);
            responseResult.setData(menuGroupsOuts);
        }
        return responseResult;
    }

    @Override
    public ResponseResult<List<QueryMenuGroupsOut>> respHandle(ResponseResult<List<QueryMenuGroupsOut>> responseResult, QueryMenuGroupsIn in) {
        return responseResult;
    }

    @Override
    public ResponseResult<List<QueryMenuGroupsOut>> listMenuGroups(QueryMenuGroupsIn in) {
        ResponseResult<List<QueryMenuGroupsOut>> result = new ResponseResult<>();
        return this.mainHandle(result, in);
    }
}
